package cn.git.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.ChineseGifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @description: 验证码工具controller
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2023-05-03
 */
@RestController
@RequestMapping("/validate")
public class ValidatePicController {

    /**
     * 生成验证码图片
     * @return
     */
    @GetMapping("/pic")
    public String pic() throws IOException, FontFormatException {
        // 图片的宽度，高度以及字符个数
        SpecCaptcha specCaptcha = new SpecCaptcha(130,48,4);
        specCaptcha.setFont(Captcha.FONT_10);
        System.out.println("随机验证码为 ： " + specCaptcha.text());

        // 将验证码图片下载到本地
        File file = new File("D:\\pic\\" + System.currentTimeMillis() + ".png");
        specCaptcha.out(new FileOutputStream(file));

        // 获取中文验证码,参数为图片的宽度，高度以及字符个数
        ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 48);
        File file2 = new File("D:\\pic\\" + System.currentTimeMillis() + ".png");
        chineseCaptcha.out(new FileOutputStream(file2));
        System.out.println("中文验证码为 ： " + chineseCaptcha.text());

        // 获取中文gif验证码
        ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 48);
        // 将验证码图片下载到本地
        File file3 = new File("D:\\pic\\" + System.currentTimeMillis() + ".gif");
        chineseGifCaptcha.out(new FileOutputStream(file3));
        System.out.println("中文gif验证码为 ： " + chineseGifCaptcha.text());

        // 算数图验证码
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        // 几位数运算，默认是两位
        captcha.setLen(3);
        // 获取运算的公式， eg: 5+6=?
        System.out.println("算数验证表达式为 ： " + captcha.getArithmeticString());
        // 获取运算的结果
        captcha.text();
        System.out.println("算数验证码为 ： " + captcha.text());
        // 将验证码图片下载到本地
        File file4 = new File("D:\\pic\\" + System.currentTimeMillis() + ".png");
        captcha.out(new FileOutputStream(file4));

        return "success";
    }

}
